package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.FoodInfo;
import com.xgkj.diyiketang.util.ImageCache;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/3/17.
 */
public class FoodListAdapter  extends BaseAdapter {
    private Context mContext;
    private List<FoodInfo> bmList = new ArrayList<FoodInfo>();
    public FoodListAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<FoodInfo> list){
        if(bmList != null){
            bmList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData(){
        if(bmList != null){
            bmList.clear();
        }
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if(bmList != null){
            return bmList.size();
        }
        return 0;
    }
    @Override
    public Object getItem(int position) {
        if(bmList != null){
            return bmList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stud
        ChildHolder holder;
        if (null == convertView) {
            holder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.bendifoodlist_item, null);
            holder.bdmoreImage = (ImageView) convertView.findViewById(R.id.bendimore_foodlogo);
            holder.bdmoreName = (TextView) convertView.findViewById(R.id.bendimore_foodname);
            convertView.setTag(holder);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        //holder.tv.setText("num" + (position + 1) + " ");
        final FoodInfo finfo = bmList.get(position);
        if(finfo!=null){
            String foodlogo = finfo.getFoodBigLogo();
            if(foodlogo != null && foodlogo.length()>0){
                ImageCache imageCache = ImageCache.getInstance(mContext);
                ViewUtil.Size size = ViewUtil.getImageSize(mContext,R.drawable.default_big_image);
                Bitmap cacheBitmap = imageCache.getBitmapFromMemoryCache(imageCache.getCacheKey(foodlogo,size));
                if ( cacheBitmap != null){
                    holder.bdmoreImage.setImageBitmap(cacheBitmap);
                }else {
                    holder.bdmoreImage.setImageResource(R.drawable.zhanweitu);
                    imageCache.loadBitmapToView(holder.bdmoreImage,foodlogo,size);
//                    ImageCache.getInstance(mContext).loadBitmapToView(holder.bdmoreImage,foodlogo, ViewUtil.getImageSize(mContext,R.drawable.default_big_image));
                }
            }
            holder.bdmoreName.setText(finfo.getfoodName());

        }
        return convertView;
    }

    class ChildHolder{
        ImageView bdmoreImage;
        TextView bdmoreName;


    }
}
